package top.carrotguo.weibo.model;

import com.orhanobut.logger.Logger;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import top.carrotguo.weibo.bean.CommentListBean;
import top.carrotguo.weibo.contract.CommentContract;
import top.carrotguo.weibo.retrofit.RetrofitUtil;
import top.carrotguo.weibo.retrofit.api.CommentService;
import top.carrotguo.weibo.retrofit.callback.BaseCallback;
import top.carrotguo.weibo.retrofit.callback.ErrorInfo;
import top.carrotguo.weibo.utils.MyConstans;

public class CommentModel implements CommentContract.Model {

    /**
     * 获取评论列表
     * @param token
     * @param id
     * @param count
     * @param page
     * @param callback
     */
    @Override
    public void getComments(Oauth2AccessToken token, String id, int count, int page, BaseCallback<CommentListBean, ErrorInfo> callback) {
        String accessToke = token.getToken();
        CommentService service = RetrofitUtil.getInstance().create(CommentService.class);
        Call<CommentListBean> call = service.getComments(accessToke,id,count,page);
        call.enqueue(new Callback<CommentListBean>() {
            @Override
            public void onResponse(Call<CommentListBean> call, Response<CommentListBean> response) {
                CommentListBean result = response.body();
                if (result != null) {
                    callback.onSuccessed(result);
                } else {
                    callback.onError(new ErrorInfo(MyConstans.REQUEST_LIMIT,"接口访问次数受限"));
                }
            }

            @Override
            public void onFailure(Call<CommentListBean> call, Throwable t) {
                Logger.e(t.getMessage());
                callback.onError(new ErrorInfo(MyConstans.CALL_FAILURE,t.getMessage()));
            }
        });
    }
}
